import java.io.*;

/*
 * Client Application to talk to the server
 * and send requests
 */
public class ClientApp {

	private Semantic semantic = null;
	
	/**
     * Accept input from user when program is first run
     * @param args[0] semantic option (1 or 2)
	 * @param args[1] freshness interval t
     */
	public static void main(String[] args) {
		int firstArg, secondArg;
		if (args.length > 0) {
			try {
				//Semantic option
				firstArg = Integer.parseInt(args[0]);
				 switch(firstArg) {
					case 1:
						System.out.println("At Least Once Semantic");
						semantic = new Semantic(1);
						break;
					case 2:
						System.out.println("At Most Once Semantic");
						semantic = new Semantic(2);
						break;
					default:
						System.err.println("No such option exist.");
						System.exit(1);
				}
				
				//Freshness interval t
				secondArg = Integer.parseInt(args[1]);
				System.out.println("Freshness interval t = " + secondArg);
			} catch (NumberFormatException e) {
				System.err.println("Argument must be an integer");
				System.exit(1);
			}
		}
		
		//User menu
        boolean run = true;
		while(run) {
			BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
			int requestOption = 0;
			System.out.println("1. Read");
			System.out.println("2. Write");
			System.out.println("3. Monitor");
			System.out.println("4. ");
			System.out.println("5. ");
			System.out.println("6. Exit");
			System.out.print("Enter option: ");
			
			try {
				requestOption = Integer.parseInt(br.readLine());
				System.out.println(""+requestOption);
				switch(requestOption) {
					case 1:
						System.out.println("Read");
						break;
					case 2:
						System.out.println("Write");
						break;
					case 3:
						System.out.println("Monitor");
						break;
					case 4:
						System.out.println("");
						break;
					case 5:
						System.out.println("");
						break;
					case 6:
						System.out.println("Exit");
						//TODO: close the server
						run = false;
						break;
					default:
						System.err.println("\nNo such option exist.\n");
				}
			} catch (NumberFormatException ex) {
				System.err.println("\nNot a valid number.\n");
			} catch (IOException e) {
				System.err.println("\nAn IO error occured.\n");
			}
		}
        
    }
}
